/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Collections;
using System.Drawing;
using System.Text;
using Core.Model.Events;
using Core.Model.Data.Generics;
using Core.Model.Data.Type;

using Core.Model.Ingredients;

namespace Core.Model.Groups
{
    public class Scene : Group
    {

        #region Exchanged Attributes
        private int inputEventRegister = 0;

        public int InputEventRegister
        {
            get { return inputEventRegister; }
            set { inputEventRegister = value; }
        }
        private Size sceneCoordinate = new Size( 720, 576 );

        public Size SceneCoordinate
        {
            get { return sceneCoordinate; }
            set { sceneCoordinate = value; }
        }
        private double aspectRatio = 0;// 4.0 / 3.0;

        public double AspectRatio
        {
            get { return aspectRatio; }
            set { aspectRatio = value; }
        }
        private bool movingCursor = false;

        public bool MovingCursor
        {
            get { return movingCursor; }
            set { movingCursor = value; }
        }

        //  todo: implement 11.1.2 nextscenes functionality

        public override void InittInternalAttributes ()
        {
            base.InittInternalAttributes();
        }
        #endregion


        #region Events


        public override void Activate ()
        {
            if ( !RunningStatus )
            {
                Engine.MHEGEngine.Instance.ActiveScene = this;
                base.Activate();
            }
        }


        public override void Destroy ()
        {
            if ( AvailabilityStatus )
            {
                base.Destroy();

                if ( Engine.MHEGEngine.Instance.ActiveScene.Equals( this ) )
                    Engine.MHEGEngine.Instance.ActiveScene = null;
            }
        }

        #endregion

        #region Actions

        public void TransitionTo ()
        {

            if ( !this.Equals( Engine.MHEGEngine.Instance.ActiveScene ) )
            {

                log.Info( "TransitionTo: "+this+" from: "+Engine.MHEGEngine.Instance.ActiveScene+"." );

                List<Ingredient> reverse = new List<Ingredient>( Engine.MHEGEngine.Instance.ActiveApplication.Items );
                reverse.Reverse();

                foreach ( Ingredient i in reverse )
                    if ( !i.Shared )
                        i.Deactivate();

                
                Engine.MHEGEngine.Instance.ClearTimers();
                
                if ( Engine.MHEGEngine.Instance.ActiveScene != null )
                {
                    Engine.MHEGEngine.Instance.ClearEventQs( Engine.MHEGEngine.Instance.ActiveScene );
                    Engine.MHEGEngine.Instance.ActiveScene.Deactivate();
                    Engine.MHEGEngine.Instance.RunActions();
                    Engine.MHEGEngine.Instance.ActiveScene.Destroy();
                    Engine.MHEGEngine.Instance.RunActions();
                }


                Prepare();
                Activate();
            }


        }

        public void SendEvent ( ObjectRef EmulatedEventSource, enumEvents EmulatedEventType, Generic EmulatedEventData )
        {
            Root source = Engine.MHEGEngine.Instance.Find( EmulatedEventSource );
            Event evnt = EventFactory.Create( source, EmulatedEventType, EmulatedEventData );

            if ( evnt.IsSynchronous )
                Engine.MHEGEngine.Instance.Handle( evnt );
            else
                Engine.MHEGEngine.Instance.AsyncHandle( evnt );

            log.Debug( "SendEvent:"+evnt+"." );
        }

        public void SetInputRegister ( int newInputRegister )
        {
            log.Debug( "SetInputRegister: "+ InputEventRegister+" => "+ newInputRegister+" ." );
            InputEventRegister = newInputRegister;

        }
        #endregion


    }
}
